<template>
  <div class="column-detail-page w-75 mx-auto">
    <div class="column-info row mb-4 border-bottom pb-4 align-items-center" v-if="column">
      <div class="col-3 text-center">
        <img :src="column.avatar && column.avatar.fitUrl" :alt="column.title" class="rounded-circle border ">
      </div>
      <div class="col-9">
        <h4>{{ column.title }}</h4>
        <p class="text-muted">{{ column.description }}</p>
      </div>
    </div>
    <PostList :list="list"></PostList>
    <!-- {{isLastPage}} -->
    <button class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25 d-block" @click="loadMorePage"
      v-if="!isLastPage">
      加载更多
    </button>
  </div>
</template>

<script setup lang="ts">
import PostList from '../components/PostList.vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import { onMounted, computed, ref } from 'vue';
import { type GlobalDataProps, ColumnProps } from '../store/store';
import { addColumnAvatar } from '../helper';
import img from '../assets/column.jpg';
import useLoadMore from '../hooks/useLoadMore';

const route = useRoute()
const store = useStore<GlobalDataProps>()

const currentId = ref<string>((route.params.id) as string)
onMounted(() => {
  store.dispatch('getColumn', currentId.value)

  store.dispatch('getPosts', onMountedParams.value)

})
const column = computed(() => {
  let seleteColum: ColumnProps = store.getters.getColumnById(currentId.value)
  if (seleteColum) {
    addColumnAvatar(seleteColum, 100, 100)
  }
  return seleteColum
})
const list = computed(() => store.getters.getPostsByCid(currentId.value))
const total = computed(() => store.getters.getTotal(currentId.value))
const currentPage = computed(() => store.getters.getCurrentPage(currentId.value))
const onMountedParams = computed(() => ({
  id: route.params.id,
  currentPage: currentPage.value ? currentPage.value : 1,
  pageSize: 3,
}))
const useLoadMoreParams = computed(() => ({
  id: (route.params.id) as string,
  currentPage: currentPage.value ? currentPage.value + 1 : 2,
  // currentPage: currentPage.value,
  pageSize: 3,
}))
const { loadMorePage, isLastPage } = useLoadMore('getPosts', total, useLoadMoreParams.value)

</script>